package entity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;
import util.Validator;

public class PassBill {

	public String id;
	public String carId;
	public String parkId;
	public String type;	
	public String entryIime;
	public String leaveIime;
	
	public List<String[]> query(String[] params) {
		List<Object> list = new ArrayList<>();
		String sql = "select id, carId, parkId, type, entryIime, leaveIime from pm_pass_bill where true ";
		if(Validator.isNotEmpty(params[0])){
			sql += "and carId like concat('%',?,'%')";
			list.add(params[0]);
		}
		sql += " order by id desc";
		try {
			List<String[]> records = DBUtil.query(sql, list.toArray(), 6);
			return records;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return new ArrayList<String[]>();
	}
	
	public List<String[]> queryStock(String[] params) {
		List<Object> list = new ArrayList<>();
		String sql = "select id, carId, parkId, type, entryIime from pm_pass_bill where entryIime is not null and leaveIime is null";
		if(Validator.isNotEmpty(params[0])){
			sql += " and carId like concat('%',?,'%')";
			list.add(params[0]);
		}
		sql += " order by id desc";
		try {
			List<String[]> records = DBUtil.query(sql, list.toArray(), 5);
			return records;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return new ArrayList<String[]>();
	}
	
	public boolean checkStock(String carId) {
		String sql = "select id, carId, parkId, type, entryIime from pm_pass_bill "
				+ "where entryIime is not null and leaveIime is null and carId = ? "
				+ "order by id desc "
				+ "limit 1";
		Object[] paraArray = {carId};
		try {
			String[] record = DBUtil.queryOne(sql, paraArray, 5);
			if(record == null){
				return false;
			}
			this.id = record[0];
			this.carId = record[1];
			this.parkId = record[2];
			this.type = record[3];
			this.entryIime = record[4];
			return true;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}
	
	public boolean entry() {
		String sql = "insert into pm_pass_bill(carId, parkId, type, entryIime) values (?,?,?,date_format(now(),'%Y-%m-%d %T'))";
		Object[] paraArray = {carId, parkId, type};
		try {
			int count = DBUtil.update(sql, paraArray);
			return count > 0;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}

	public boolean leave() {
		String sql = "update pm_pass_bill set leaveIime = date_format(now(),'%Y-%m-%d %T') where id = ?";
		Object[] paraArray = {id};
		try {
			int count = DBUtil.update(sql, paraArray);
			return count > 0;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}
	
	public boolean load() {
		String sql = "select carId, parkId, type entryIime, leaveIime from pm_pass_bill where id = ?";
		Object[] paraArray = {id};
		try {
			String[] record = DBUtil.queryOne(sql, paraArray, 5);
			if(record == null){
				return false;
			}
			carId = record[0];
			parkId = record[1];
			type = record[2];
			entryIime = record[3];
			leaveIime = record[4];
			return true;
		} catch (SQLException e) {
			DBUtil.error(e);
		}
		return false;
	}
	
}

